#include "process_pool.h"

int make_child(process_data_t *p_manage,int process_num)
{
    int i,ret;
    pid_t pid;
    int fds[2];//这个是与子进程之间的管道
    for(i=0;i<process_num;i++)//创建多个子进程
    {
        ret=socketpair(AF_LOCAL,SOCK_STREAM,0,fds);
        ERROR_CHECK(ret,-1,"socketpair")
        pid=fork();
        if(0==pid)
        {
            close(fds[1]);
            child_handle(fds[0]);//一定要让子进程在child_handle内exit
        }
        close(fds[0]);
        p_manage[i].pid=pid;
        p_manage[i].fd=fds[1];
        p_manage[i].busy=0;//默认非忙碌状态
#ifdef DEBUG
        printf("p_manage[%d].fd=%d\n",i,p_manage[i].fd);
#endif
    }
}

int child_handle(int fd)
{
    int new_fd,ret,exit_flag;//子进程要接收一个new_fd，通过new_fd给对应的客户端发文件   
    char flag=1;
    while (1)
    {
        recv_fd(fd,&new_fd,&exit_flag);//接收任务
        if(exit_flag)
        {
            printf("child %d will exit\n",getpid());
            exit(0);
        }
        printf("gets task,new_fd=%d\n",new_fd);
        tran_file(new_fd);//发文件给客户端
        close(new_fd);
        write(fd,&flag,sizeof(flag));//通知父进程完成任务
    } 
    return 0;
}